﻿using System;
using System.Text;
using System.Web.Mvc;
using ActivateYourGlutes.Infrastructure.Logging;
using ActivateYourGlutes.Payloads.Home;
using ActivateYourGlutes.Services;
using ActivateYourGlutes.Services.Model;

namespace ActivateYourGlutes.Controllers
{
    public partial class HomeController : LocalisingController
    {
        private readonly IBodyProvider _bodyProvider;
        private readonly IAccountProvider _accountProvider;
        private readonly IGoalProvider _goalProvider;

        public HomeController(
            IBodyProvider bodyProvider,
            IAccountProvider accountProvider,
            IGoalProvider goalProvider,
            ILogger logger) : base(accountProvider, logger)
        {
            _bodyProvider = bodyProvider;
            _accountProvider = accountProvider;
            _goalProvider = goalProvider;
        }

        public virtual ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ActivateYourGlutes";
            var account = CurrentAccount;
            if (account != null)
            {
                var payload = new IndexPayload {PrimarySeriesXaml = "", SecondarySeriesXaml = ""};
                if (_accountProvider.IsDataLoggedForUser(User))
                {
                    BuildIndexPayloadForRegisteredUser(account, payload);
                }
                else
                {
                    payload.AccountHasData = false;
                }
                ViewData.Model = payload;
            }

            return View();
        }

        private void BuildIndexPayloadForRegisteredUser(Account account, IndexPayload payload)
        {
            payload.RecentActivity = _accountProvider.RecentActivity(User);
            payload.AccountHasData = true;
            payload.ActiveGoals = _goalProvider.ActiveGoals(account);
            payload.AchievedGoals = _goalProvider.RecentlyAchievedGoals(account);
            if (account.PrimaryMeasurementType != null)
            {
                var primaryIndicatorSeries = _bodyProvider.PrimaryIndicatorSeries(account);
                var seriesOne = new StringBuilder();
                seriesOne.Append("<vc:DataSeries LegendText=\"");
                seriesOne.Append(primaryIndicatorSeries.Title);
                seriesOne.Append("\" RenderAs=\"Line\" AxisYType=\"Primary\" XValueType=\"DateTime\" XValueFormatString=\"d MMM yyyy\" >");
                seriesOne.Append("<vc:DataSeries.DataPoints>");

                foreach (var seriesValuePair in primaryIndicatorSeries.Values)
                {
                    if (seriesValuePair.Y != null)
                    {
                        seriesOne.Append("<vc:DataPoint XValue=\"");
                        seriesOne.Append(seriesValuePair.XDate.ToString("MM/dd/yyyy"));
                        seriesOne.Append("\" YValue=\"");
                        seriesOne.Append(seriesValuePair.Y);
                        seriesOne.Append("\" />");
                    }
                }

                seriesOne.Append("</vc:DataSeries.DataPoints>");
                seriesOne.Append("</vc:DataSeries>");

                payload.PrimarySeriesTitle = primaryIndicatorSeries.Title;
                payload.PrimarySeriesXaml = seriesOne.ToString();
            }

            if (account.SecondaryMeasurementType != null)
            {
                var secondarySeriesIndicator = _bodyProvider.SecondaryIndicatorSeries(account);
                var series = new StringBuilder();
                series.Append("<vc:DataSeries LegendText=\"");
                series.Append(secondarySeriesIndicator.Title);
                series.Append("\" RenderAs=\"Line\" AxisYType=\"Secondary\" XValueType=\"DateTime\" XValueFormatString=\"d MMM yyyy\" >");
                series.Append("<vc:DataSeries.DataPoints>");

                foreach (var seriesValuePair in secondarySeriesIndicator.Values)
                {
                    if (seriesValuePair.Y != null)
                    {
                        series.Append("<vc:DataPoint XValue=\"");
                        series.Append(seriesValuePair.XDate.ToString("MM/dd/yyyy"));
                        series.Append("\" YValue=\"");
                        series.Append(seriesValuePair.Y);
                        series.Append("\" />");
                    }
                }

                series.Append("</vc:DataSeries.DataPoints>");
                series.Append("</vc:DataSeries>");

                payload.SecondarySeriesTitle = secondarySeriesIndicator.Title;
                payload.SecondarySeriesXaml = series.ToString();
            }
        }

        public virtual ActionResult About()
        {
            return View();
        }

        public virtual ActionResult Error()
        {
            return View();
        }
    }
}
